import React from 'react'; import { View, Text, Animated, Easing } from 'react-native'; import type { ChooseAnswerWidgetParamsType, WidgetPositionLimitsType, WidgetPositionType, } from '../../../types'; import { styles } from './styles'; import { Answer } from './Answer'; import { stylesUtils } from '../../../utils'; import Reactions from '../../../core/Reactions'; interface Props { params: ChooseAnswerWidgetParamsType; position: WidgetPositionType; positionLimits: WidgetPositionLimitsType; widgetId: string; } const INIT_ELEMENT_STYLES = { container: { borderRadius: 10, }, header: { fontSize: 12, paddingTop: 13, paddingBottom: 13, }, answers: { padding: 12, }, answer: { padding: 8, marginBottom: 6, }, answerId: { width: 18, height: 18, marginRight: 8, fontSize: 10, }, answerTitle: { fontSize: 10, }, }; export const ChooseAnswerWidget: React.FC = ({ params, position, positionLimits, widgetId, }) => { const { text, answers, correct } = params; const shakeAnimation = new Animated.Value(0); const celebrateAnimation = new Animated.Value(0); const [userAnswer, setUserAnswer] = React.useState(null); const handleMarkAnswer = (answerId: string) => () => { if (!userAnswer) { setUserAnswer(answerId); Reactions.registerWidget(widgetId); Reactions.send('answer', answerId); } }; React.useEffect(() => { if (!!userAnswer && userAnswer !== correct) { Animated.timing(shakeAnimation, { toValue: 1, duration: 1000, easing: Easing.linear, useNativeDriver: true, }).start(); } if (!!userAnswer && userAnswer === correct) { Animated.timing(celebrateAnimation, { toValue: 1, duration: 1000, easing: Easing.linear, useNativeDriver: true, }).start(); } }, [celebrateAnimation, correct, shakeAnimation, userAnswer]); const calculate = (size: number) => { if (position && positionLimits) { return stylesUtils.calculateElementSize(position, positionLimits, size); } return size; }; const elementSizes = React.useMemo( () => ({ container: { borderRadius: calculate(INIT_ELEMENT_STYLES.container.borderRadius), }, header: { paddingTop: calculate(INIT_ELEMENT_STYLES.header.paddingTop), paddingBottom: calculate(INIT_ELEMENT_STYLES.header.paddingBottom), }, title: { fontSize: calculate(INIT_ELEMENT_STYLES.header.fontSize), }, answers: { padding: calculate(INIT_ELEMENT_STYLES.answers.padding), }, answer: { padding: calculate(INIT_ELEMENT_STYLES.answer.padding), marginBottom: calculate(INIT_ELEMENT_STYLES.answer.marginBottom), }, answerId: { width: calculate(INIT_ELEMENT_STYLES.answerId.width), height: calculate(INIT_ELEMENT_STYLES.answerId.height), marginRight: calculate(INIT_ELEMENT_STYLES.answerId.marginRight), }, answerIdText: { fontSize: calculate(INIT_ELEMENT_STYLES.answerId.fontSize), }, answerTitle: { fontSize: calculate(INIT_ELEMENT_STYLES.answerTitle.fontSize), }, }), [calculate] ); return ( {text} {answers.map(({ id, title }) => ( ))} ); };